import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/public/public_date.dart';
import 'package:honeypotflutter/public/public_picker.dart';
import 'package:honeypotflutter/publicBussine/bussine_date.dart';
import '../../help/cabinet_help.dart';

class CabinetFromBottomCustomLogic extends GetxController {
  String startTime = "";
  String endTime = "";

  /*
  * 选择时间按钮
  * */
  selectCheck(CabinetDateType type) {
    int month = 0;
    switch (type) {
      case CabinetDateType.threeMonth:
        month = -3;
        break;
      case CabinetDateType.halfYear:
        month = -6;
        break;
      case CabinetDateType.oneYear:
        month = -12;
        break;
    }
    BussineSelectDateModel date = MGBussineDate.calculateMonthDate(month);
    startTime = date.startTime;
    endTime = date.endTime;
    update();
  }

  /*
   选择时间
  */
  selectDatePicker(CabinetDateCustomType type) {
    if (type == CabinetDateCustomType.startTime) {
      PDuration initDate = PDuration.now();
      if (startTime.isNotEmpty) {
        DateTime time = HLDate.hl_stringToDateTime(startTime);
        initDate = PDuration(year: time.year, month: time.month, day: time.day);
      }
      PDuration endDate = PDuration.now();
      if (endTime.isNotEmpty) {
        DateTime time = HLDate.hl_stringToDateTime(endTime);
        endDate = PDuration(year: time.year, month: time.month, day: time.day);
      }
      HLPicker.showDatePicker(
        selectDate: initDate,
        maxDate: endDate,
        onConfirm: (value) {
          startTime = "${value.year}-${formatString(value.month)}-${formatString(value.day)}";
          update();
        },
      );
      return;
    }

    PDuration initDate = PDuration.now();
    if (endTime.isNotEmpty) {
      DateTime time = HLDate.hl_stringToDateTime(endTime);
      initDate = PDuration(year: time.year, month: time.month, day: time.day);
    }
    PDuration? startDate;
    if (startTime.isNotEmpty) {
      DateTime time = HLDate.hl_stringToDateTime(startTime);
      startDate = PDuration(year: time.year, month: time.month, day: time.day);
    }
    HLPicker.showDatePicker(
      selectDate: initDate,
      minDate: startDate,
      maxDate: PDuration.now(),
      onConfirm: (value) {
        DateTime time = HLDate.hl_yymmddToDateTime(year: value.year, month: value.month, day: value.day);
        endTime = "${value.year}-${formatString(value.month)}-${formatString(value.day)}";
        update();
      },
    );
  }

  String formatString(int? value) {
    if (value == null) {
      return "00";
    }
    if (value >= 10) {
      return value.toString();
    } else {
      return "0${value}";
    }
  }

  /*
  * 重置
  * */
  reset() {
    startTime = "";
    endTime = "";
    update();
  }
}
